namespace ScrumSprintMonitor.Modules.BuildMonitor.ViewModels
{
	#region #using Directives

	using Infrastructure;

	using Microsoft.Practices.ServiceLocation;

	using UI.Wpf.ViewModels;

	#endregion

	public abstract class BuildViewModelBase : ViewModelBase
	{
		private readonly IServiceLocator serviceLocator;
		private IReportableData reportableData;

		protected BuildViewModelBase(IServiceLocator serviceLocator)
		{
			this.serviceLocator = serviceLocator;
		}

		#region Protected Properties

		protected IReportableData ReportableData
		{
			get { return this.reportableData; }
			set
			{
				if (value != this.reportableData)
				{
					this.reportableData = value;

					OnPropertyChanged("ReportableData");
				}
			}
		}

		protected IBuildInfo LastBuild
		{
			get
			{
				EnsureConnected();
				return (ReportableData != null ? ReportableData.LastBuild : null);
			}
		}

		#endregion

		#region Protected Methods

		protected void EnsureConnected()
		{
			EnsureConnected(false);
		}

		protected virtual void EnsureConnected(bool throwException)
		{
			GuardDisposed();

			if (ReportableData == null && this.serviceLocator != null)
			{
				ReportableData = this.serviceLocator.GetInstance<IReportableData>();
			}
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				ReportableData = null;
			}

			base.Dispose(disposing);
		}

		#endregion
	}
}